// 牌堆动画管理
var MaJiang = require('MaJiang');
import Defines from './../MJ/defines'

cc.Class({
    extends: cc.Component,

    properties: {
        m_PlayerNode0 : [cc.Node],          // 自己的
        m_PlayerNode1 : [cc.Node],          // 左手
        m_PlayerNode2 : [cc.Node],          // 对面
        m_PlayerNode3 : [cc.Node],          // 右手
        m_ShouPai : cc.Node,                // 手牌
        m_PlayerShou0 : [cc.Button],        // 自己的手牌
        m_ShowPai : cc.SpriteAtlas,         // 自己的手牌图集
        m_PlayerShou1 : [cc.Node],          // 左手
        m_PlayerShou2 : [cc.Node],          // 对面
        m_PlayerShou3 : [cc.Node],          // 右手
        m_PlayerShou0fan : [cc.Node],       // 自己手上最后一张牌
        m_cardsDianJi : cc.Node,            // 牌不可点击
        m_PlayrID : 0,                      // 玩家牌堆的ID
        m_CardID : 0,                       // 牌堆的数量
        m_TakePlayrID : 0,                  // 拿牌玩家牌堆的ID
        m_TakeCardID : 0,                   // 拿牌的位置
        m_numss : 0,                        // 4张发牌的标记
        m_zhuangSeat : 0,                   // 庄家座位号
    },

    start () {
        this.m_cardsDianJi.active = true;

        //var data = {0x01:"card_11", 0x02:"card_12", 0x03:"card_13", 0x04:"card_14"};
        //var data = {1:0x01, 2:0x02, 3:0x03, 4:0x04, 5:0x05};
        // this.data = [0x01,0x02,0x13,0x04,0x05,0x01,0x21,0x22];

        // for (var num = 0; num < this.data.length; num++)
        // {
        //     Defines.playerData.info.push(this.data[num]);
        // }
       
        // 设置牌的值
        this.initPlayerDice(Defines.playerData.info);

        // 初始化色子点数和发牌
        this.initDicePoint(Defines.playerData.PalyerDice, Defines.playerData.PalyerDicePoint);
    },

    // 设置牌的值
    initPlayerDice : function(diceData) {
        // 手上的牌设置牌值
        for (var num = 0; num < diceData.length; num++) {
            var card = this.m_PlayerShou0[num].node.getChildByName("card").getComponent(cc.Sprite);
           // MaJiang.cardList(diceData);
            var frame = this.m_ShowPai.getSpriteFrame(MaJiang.cardPng(diceData[num]));                                                          /////////////////////////////////改
            card.spriteFrame = frame;
        }
    },

    // 牌整理排序
    SetListPlayerDice : function(diceData) {
        // 手上的牌设置牌值
        MaJiang.cardList(diceData);
        for (var num = 0; num < diceData.length; num++) {
            var card = this.m_PlayerShou0[num].node.getChildByName("card").getComponent(cc.Sprite);
            var frame = this.m_ShowPai.getSpriteFrame(MaJiang.cardPng(diceData[num]));                                                         /////////////////////////////////改
            card.spriteFrame = frame;
            card.paiData = diceData[num];
        }
        
        this.m_cardsDianJi.active = false;
    },

    // 初始化色子点数和发牌
    // playerID             拿牌的玩家
    // paintNum             色子点数
    initDicePoint : function(playerID, paintNum) {
        // 初始化(第一个投色子的玩家ID，色子的点数）
        this.SetPlayerDicePoint(playerID, paintNum);

        // 各多少时间执行函数（4张拿牌）***********************************
        this.seatNum = this.m_zhuangSeat; // 庄家座位号
        this.numCi2 = 0;
        let action1 = cc.delayTime(1);
        let actionZJ1 = cc.callFunc(() => {
            this.SetDisappearCardFour(this.seatNum,4);
            this.seatNum+=1;
            if(this.seatNum >= 4)
            {
                this.seatNum = 0;
                this.numCi2 += 1;
            }
        }, this);

        this.numCi3 = 0;
        // 各多少时间执行函数（单张）***************************************
        this.numCi11 = this.m_zhuangSeat;
        let actionZJ2 = cc.callFunc(() => {
            var num = 1;
            if(this.numCi11 == this.m_zhuangSeat)
            {
                num = 2;
            }
            this.SetDisappearCardDan(this.numCi11, num);
            this.numCi11+=1;
            this.numCi3+=1;
            if(this.numCi11 >= 4)
            {
                this.numCi11 = 0;
            }
        }, this);

        // 牌整理******************************************
        let actionZJ21 = cc.callFunc(() => {
            this.SetListPlayerDice(Defines.playerData.info);
            this.m_PlayerShou0fan[1].active = false;
            this.m_PlayerShou0fan[2].active = true;
        }, this);

        let actionZJ22 = cc.callFunc(() => {
            this.m_PlayerShou0fan[1].active = true;
            this.m_PlayerShou0fan[2].active = false;
        }, this);

        let action6 = cc.callFunc(() => {
            if(this.numCi3 == 4)
            {
                this.numCi2 = 0;
                this.node.runAction(cc.repeat(cc.sequence(action1, actionZJ21, action1, actionZJ22), 1));
            }
        }, this);

        let action5 = cc.callFunc(() => {
            if(this.numCi2 == 3)
            {
                this.numCi2 = 0;
                this.node.runAction(cc.repeat(cc.sequence(action1, actionZJ2, action6), 4));
            }
        }, this);
        // 执行12次
        this.node.runAction(cc.repeat(cc.sequence(action1, actionZJ1, action5), 12));
    },
    
    // 牌堆拿掉的牌消失（单张拿牌）
    SetDisappearCard : function() {
        // 牌堆的牌消失
        if(0 == this.m_PlayrID)
        {
            this.m_PlayerNode0[this.m_CardID].active = false;
        }
        else if(1 == this.m_PlayrID)
        {
            this.m_PlayerNode1[this.m_CardID].active = false;
        }
        else if(2 == this.m_PlayrID)
        {
            this.m_PlayerNode2[this.m_CardID].active = false;
        }
        else if(3 == this.m_PlayrID)
        {
            this.m_PlayerNode3[this.m_CardID].active = false;
        }

        // 牌堆的牌消失后如果牌堆的牌拿完了就结束游戏
        if(this.m_TakeCardID == this.m_CardID && this.m_PlayrID == this.m_TakePlayrID)
        {
            return false;
        }
        
        // 没拿完自动后加
        this.m_CardID+=1;
        if(this.m_CardID == 34)
        {
            this.m_PlayrID += 1;
            // 开始牌堆的ID
            if(4 <= this.m_PlayrID)
            {
                this.m_PlayrID = 0;
            }
            this.m_CardID = 0;
        }

        // 玩家背面牌堆的ID
        Defines.playerData.PaiDuiPlayrID = this.m_PlayrID;
        // 背面牌堆的数量
        Defines.playerData.PaiDuiCardNum = this.m_CardID;

        return true;
    },

    // 计算色子点数，拿牌的位置
    // playrID              拿牌位置玩家ID
    // dicePoint            色子点数
    SetPlayerDicePoint : function(playrID ,dicePoint){
        this.SetShow();
        // 手牌显示
        this.SetShouCardInit();
        this.m_ShouPai.active = true;

        this.m_PlayrID = playrID;
        
        // 点数大于牌堆就轮到到下一家开始拿牌
        if(34 < dicePoint*2)
        {
            this.m_PlayrID += 1;
            this.m_CardID = dicePoint*2 - 34 - 2; // 减2是开始拿牌的位置
        }
        else
        {
            this.m_CardID = dicePoint*2 - 2;// 减2是开始拿牌的位置
        }

        // 开始牌堆的ID
        if(4 <= this.m_PlayrID)
        {
            this.m_PlayrID = 0;
        }

        if(this.m_CardID < 0)
        {
            this.m_CardID = 34;
        }
        // 记录开始拿牌玩家的牌堆ID
        this.m_TakePlayrID = this.m_PlayrID;
        // 记录开始拿牌玩家的牌堆拍的位置
        this.m_TakeCardID = this.m_CardID-1;

        Defines.playerData.KaiShiTakePlayrID = this.m_TakePlayrID;
        Defines.playerData.KaiShiTakeCardID = this.m_TakeCardID;
    },

    // 全部显示
    SetShow : function(){
        for(var num = 0; num < 30; ++num)
        {
            this.m_PlayerNode0[num].active = true;
            this.m_PlayerNode1[num].active = true;
            this.m_PlayerNode2[num].active = true;
            this.m_PlayerNode3[num].active = true;
        }

        this.m_PlayrID = 0;                 // 玩家牌堆的ID
        this.m_CardID = 0;                  // 牌堆的数量
        this.m_TakePlayrID = 0;             // 拿牌玩家牌堆的ID
        this.m_TakeCardID = 0;              // 拿牌的位置
    },

    // 手牌
    SetShouCardInit : function(){
        for(var num = 0; num < 14; ++num)
        {
            this.m_PlayerShou0[num].node.active = false;
            this.m_PlayerShou1[num].active = false;
            this.m_PlayerShou2[num].active = false;
            this.m_PlayerShou3[num].active = false;
        }
    },

    // 发牌牌堆拿掉的牌消失（4张拿牌）
    SetDisappearCardFour : function(playrID ,ZhangNum){
        for(var num = 0; num < ZhangNum; ++num)
        {
            this.SetDisappearCard();
        }

        if(playrID == this.m_zhuangSeat)
        {
            this.m_numss+=1;
        }
        this.SetShowShouCard(playrID,this.m_numss);
    },

    // 手牌显示(4张)
    SetShowShouCard : function(playrID,showNum){
        for(var num = 0; num < 14; ++num)
        {
            if(num < showNum*4 && playrID == 0)
            {
                this.m_PlayerShou0[num].node.active = true;
            }
            else if(num < showNum*4 && playrID == 1)
            {
                this.m_PlayerShou1[num].active = true;
            }
            else if(num < showNum*4 && playrID == 2)
            {
                this.m_PlayerShou2[num].active = true;
            }
            else if(num < showNum*4 && playrID == 3)
            {
                this.m_PlayerShou3[num].active = true;
            }
        }
    },

    // 发牌牌堆拿掉的牌消失（1张拿牌）
    SetDisappearCardDan : function(playrID ,ZhangNum){
        for(var num = 0; num < ZhangNum; ++num)
        {
            this.SetDisappearCard();
        }
        var boolZhuang = false;
        if(ZhangNum > 1)
        {
            boolZhuang = true;
        }
        this.SetShowShouCardDan(playrID ,boolZhuang);
    },

    // 手牌显示(单张)
    SetShowShouCardDan : function(playrID ,showNum){
        if(showNum)
        {
            for(var num = 0; num < 14; ++num)
            {
                if(playrID == 0)
                {
                    this.m_PlayerShou0[num].node.active = true;
                    this.m_PlayerShou0fan[0].active = true;
                }
                else if(playrID == 1)
                {
                    this.m_PlayerShou1[num].active = true;
                }
                else if(playrID == 2)
                {
                    this.m_PlayerShou2[num].active = true;
                }
                else if(playrID == 3)
                {
                    this.m_PlayerShou3[num].active = true;
                }
            }
        }
        else
        {
            for(var num = 0; num < 14; ++num)
            {
                if(num < 13 && playrID == 0)
                {
                    this.m_PlayerShou0[num].node.active = true;
                    this.m_PlayerShou0fan[0].active = false;
                }
                else if(num < 13 && playrID == 1)
                {
                    this.m_PlayerShou1[num].active = true;
                }
                else if(num < 13 && playrID == 2)
                {
                    this.m_PlayerShou2[num].active = true;
                }
                else if(num < 13 && playrID == 3)
                {
                    this.m_PlayerShou3[num].active = true;
                }
            }
        }
    },
});
